/**!
 * @license UL2Dtree Parser
 * GPL+MIT Dual (http://www.shamasis.net/)
 * @author: Shamasis Bhattacharya
 * @description: UL to DTree Converter
 * @version: 2.0.0-beta
 */

var UL2DTree = new function() { };

UL2DTree.mutex = { l: 0 };

/**
 * This is the core UL2DTree parser. It performs all routines pertinent to
 * convering an UL into dtree
 */
UL2DTree.parser = {

    /**
     * Initiates parsing of a particular UL and creats the basic DTree object
     * for the same.
     * Dtree object is returned with the required nodes added to it.
     *
     * @param id string : The DOM ID of the UL element which is to be converted
     * @param name string : (optional) Name of the root element
     *
     * @return object
     */
    root: function(id, name) {

        // load source object
        var ob = UL2DTree.lib.g(id);
        
        // create new dtree
        var dt = new dTree(id);

        // create root node
        dt.add(UL2DTree.mutex.l, -1, name || '');
 
        // parse lists
        UL2DTree.parser.ul(ob, dt, 0);

        // hide source list
        ob.style.display='none';

        // return dtree
        return dt;
    },

    /**
     * Parses an UL element and initiates the parsing of each list element that
     * is a root to this UL.
     *
     * @param ul object : The HTMLULNode element that has to be parsed
     * @param dt object : The DTree object associated with this UL
     * @param pt string : Pointer to the Parent Node
     * @return void
     */
    ul: function(ul, dt, pt) {

        // get all items in root
        var nl = ul.childNodes;

        // loop through items to process the list items only
        for(var i=0; i<nl.length; i++) {

            // continue loop when non-li item found
            if( nl[i].nodeName.toLowerCase() != 'li' ) continue;
            
            // initiate li parser
            UL2DTree.parser.li(nl[i], dt, pt);
        }
    },


    /* Parses an LI element and adds a dtree node for every element. Initiates
     * Parsing of inner UL elements
     *
     * @param li object : The HTMLLINode element that has to be parsed
     * @param dt object : The DTree object associated with this UL
     * @param pt string : Pointer to the Parent Node
     * @return void
     */
    li: function(li, dt, pt)
    {
        // forward insertion point
        UL2DTree.mutex.l++;

        // get the root anchor or the anchor text (or null if nothing found)
        var an = UL2DTree.lib.r('a', li);

        // prepare dtree parameters
        var pm = an ?
            // set parameters when there is a true anchor
            [na.innerHTML, // node value
                ''+an, // node link
                UL2DTree.lib.a('title', an) || UL2DTree.lib.a('title', li) // tooltip
            ] :
            // when no anchor was found
            [li.childNodes[0].nodeValue.toString(), // node value
                null, // node link
                UL2DTree.lib.a('title', li) // tooltip
            ];

        dt.add(UL2DTree.mutex.l, pt, pm[0], pm[1], pm[2]);

        // recurse sublists to create deeper tree
        var ul = UL2DTree.lib.r('ul', li);

        // in case ul exists start recursion
        if(ul) UL2DTree.parser.ul(ul, dt, UL2DTree.mutex.l);
    }

};

UL2DTree.lib = {
    g: function(i, o) { return (o || document).getElementById(i); },
    a: function(i, o) { return o.getAttribute(i); },
    t: function(i, o) { return (o || document).getElementsByTagName(i); },
    e: function(m) { alert(m); },

    r: function(i, o) {
        var xx=UL2DTree.lib.t(i, o);
        return (xx.length) ? (xx[0].parentNode==o) ? xx[0] : null : null; }
};

UL2DTree.res = { };
